#ifndef ATTACK_H
#define ATTACK_H
#include <cstdlib>
class Player;
class Monster;

//controller feel free to modify to suite your needs.		
namespace Attack{
	static int doAttack(Player* p, Monster* m, const AttackType& a)
	{
		int chanceToEvade = m->getMonsterStats().getAgility() / 25;  // every 25 pts of agility gives 1% chance to evade.
		
		if( (rand() % 100) < chanceToEvade )
		{
		cout << "monster evades attack" << endl;
			return 0;		
		}
		
		int attackTypeBaseDamage = 0;      // assume default attack type base damage is 0.   
		
		int attackActionCost   = a.getActionPointsCost();
		int playerActionPoints = p->getPlayerStats().getActionPoints();
		  
		if( playerActionPoints >= attackActionCost )  // if player has enough AP to peform attack
		{
			int playerActionPointsAfterAttack = playerActionPoints - attackActionCost;
			p->getPlayerStats().setActionPoints( playerActionPointsAfterAttack );
			
			attackTypeBaseDamage = a.getBaseDamage();  // apply attack damage, otherwise defaults to 0.
		}			

		int playerBonusStat = 0;
		if( p->getCharClass() == WARRIOR )
			playerBonusStat = p->getPlayerStats().getStrength();
		else if( p->getCharClass() == WIZARD )
			playerBonusStat = p->getPlayerStats().getIntelligence();
		else 
			playerBonusStat = p->getPlayerStats().getConcentration();
		
		int playerDamage   = p->getPlayerStats().getDamage();
		int weightedDamage = playerDamage + attackTypeBaseDamage + (playerBonusStat / 20);   // +5% bonus damage of player's special stat
		int mitigateDamage = weightedDamage - m->getMonsterStats().getArmor();
		
		int monsterHitPointAfterAttack = m->getMonsterStats().getHitPoints() - mitigateDamage;
		m->getMonsterStats().setHitPoints( monsterHitPointAfterAttack );
		
		if( monsterHitPointAfterAttack <= 0 )
			m->setDead( true );
        
        return mitigateDamage;
	}

	static int doAttack(Monster* m, Player* p)
	{
		int chanceToEvade = p->getPlayerStats().getAgility() / 25;  // every 25 pts of agility gives 1% chance to evade.
		
		if( (rand() % 100) < chanceToEvade )
		{
		cout << "player evades attack" << endl;
			return 0;		
		}

		int attackTypeBaseDamage = 0;      // assume default attack type base damage is 0.   
		
		int r = rand() % m->getNumAttacks();  // random attack type r
		
		AttackType anAttackType;
		if( m->getAttackType(r, anAttackType) )  // will probably be true all the time, just in case vector.size() = 0;
		{
			int attackActionCost    = anAttackType.getActionPointsCost();
			int monsterActionPoints = m->getMonsterStats().getActionPoints();
			  
			if( monsterActionPoints >= attackActionCost )  // if monster has enough AP to peform attack
			{
				int monsterActionPointsAfterAttack = monsterActionPoints - attackActionCost;
				m->getMonsterStats().setActionPoints( monsterActionPointsAfterAttack );
				
				attackTypeBaseDamage = anAttackType.getBaseDamage();  // apply attack damage, otherwise defaults to 0.
			}			
		}
		
		int weightedDamage = m->getMonsterStats().getDamage() + attackTypeBaseDamage; 
		int mitigateDamage = weightedDamage - p->getPlayerStats().getArmor();
		
		int playerHitPointAfterAttack = p->getPlayerStats().getHitPoints() - mitigateDamage;
		p->getPlayerStats().setHitPoints( playerHitPointAfterAttack );
		
		if( playerHitPointAfterAttack <= 0 )
			p->setDead( true );
        
        return mitigateDamage;
	}
}
#endif
